import {GETLIST,UPTADECHOSENCONTACTID,UPTADEEDITINGCONTACT} from  "./mutation_types"
import $api from "../api/api"
const OK = 200;
export default {
  async getList({commit},options={}){
    // const {code,data} = await $api.get("/contactList")
    const {code,data} = await $api.contact.getContactList({a:"a"},{...options})

    console.log(data);

    if(code === OK)
      commit(GETLIST,data)

    return 123
  },

  async addList({commit,dispatch},info){
    // const {code,data} = await $api.post("/contact/new/json",info);

    const {code,data} = await $api.contact.addContactByForm(info)
    // const {code,data} = await $api.contact.addContactByJson(info)
    if(code===OK){
      await dispatch(GETLIST,{loading:{need:true}})
      commit(UPTADECHOSENCONTACTID,data.id)
    }
  },

  async updateList({commit,dispatch},info){
    // const {code,data} =  await $api.put("/contact/edit",info);
    const {code,data} = await $api.contact.updateContact(info)
    if(code===OK){
      await dispatch(GETLIST,{loading:{need:false}})
      commit(UPTADECHOSENCONTACTID,data.id)
    }
  },

  async delList({commit,dispatch,state},id){
    // const {code} = await $api.delete("/contact",{params:{id}});
    const {code,data} = await $api.contact.deleteContact({id})
    if(code===OK){
      if (state.chosenContactId === id) {
        commit(UPTADECHOSENCONTACTID,null)
      }
      await dispatch(GETLIST,{loading:{need:false}})
    }
  },


  updateEditingContact({commit},editingContact){
    commit(UPTADEEDITINGCONTACT,editingContact)
  },

  updateChosenContactId({commit},chosenContactId){
    commit(UPTADECHOSENCONTACTID,chosenContactId)
  }
}
